//
//  BanBu_RecommentViewController.m
// Halfarea
//
//  Created by banbu3 on 14-3-17.
//
//

#import "BanBu_RecommentViewController.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "BanBuAPIs.h"
#import "LeveyTabBarController.h"
#import "BanBu_PersonDataViewController.h"
static BanBu_RecommentViewController *shareRecomment = nil;
@interface BanBu_RecommentViewController ()
{
    NSMutableDictionary *useridListDic;
    UIButton *guanzhuButton;
}
@end

@implementation BanBu_RecommentViewController

+(BanBu_RecommentViewController *)shareInstance
{
    @synchronized(self)
    {
        if(shareRecomment == nil)
        {
            shareRecomment = [[self alloc] init];
            [shareRecomment initResource];
            
        }
    }
    return shareRecomment;
}
-(void)initResource
{
    _recommentArray = [[NSMutableArray alloc] initWithCapacity:0];
    useridListDic   = [[NSMutableDictionary alloc] initWithCapacity:0];
    
    
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor clearColor];
    [self getRecommentPerson];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    aview.backgroundColor = [UIColor clearColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, 5, 280, 50);
    [btn setBackgroundImage:[UIImage imageNamed:@"startverify.png"] forState:UIControlStateNormal];
    [btn setTitle:NSLocalizedString(@"oneStepFollow", nil) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(oneStepFollowAll) forControlEvents:UIControlEventTouchUpInside];
    guanzhuButton = btn;
    [aview addSubview:btn];
    self.tableView.tableFooterView = aview;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)getRecommentPerson
{
    [AppComManager getBanBuData:BanBu_Get_Video_Verify par:[NSDictionary dictionary] delegate:self];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [guanzhuButton setBackgroundImage:[UIImage imageNamed:@"startverify.png"] forState:UIControlStateNormal];
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated
{
    [guanzhuButton setBackgroundImage:[UIImage imageNamed:@"startverify.png"] forState:UIControlStateNormal];
}
-(void)oneStepFollowAll
{
    [guanzhuButton setBackgroundImage:[UIImage imageNamed:@"startverify.png"] forState:UIControlStateNormal];
    NSString *useridList = [[useridListDic allKeys] componentsJoinedByString:@","];
    [AppComManager getBanBuData:BanBu_Link_User_Tomany_Onekey par:[NSDictionary dictionaryWithObjectsAndKeys:useridList,@"touidlist", nil] delegate:self];
    [TKLoadingView showTkloadingAddedTo:self.view point:CGPointMake(160, 100)  title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tableView release],_tableView = nil;
    [_recommentArray release],_recommentArray = nil;
    [useridListDic release],useridListDic = nil;
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTableView:nil];
    [super viewDidUnload];
}
#pragma mark -
#pragma mark Table view data source
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{

//}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 60;
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //每次多获取20条供下一页准备
    if([self.recommentArray count]%4!=0)
    {
        gridViewRow = ([self.recommentArray count])/4+1;
    }
    else
    {
        gridViewRow = ([self.recommentArray count])/4;
    }
    
    if(gridViewRow>4){gridViewRow = 4;}
    
    return gridViewRow;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 79;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *GridCellIdentifier = @"GridCellIdentifier";
    BanBu_GridCell *cell = (BanBu_GridCell *)[tableView dequeueReusableCellWithIdentifier:GridCellIdentifier];
    if(cell == nil)
    {
        cell = [[[BanBu_GridCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GridCellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
    }
    
    if(cell.contentView.subviews.count){
        for(UIButton *temp in cell.contentView.subviews){
            temp.hidden = YES;
        }
    }
    cell.backgroundColor = [UIColor clearColor];
    if(gridViewRow-1 == indexPath.row && self.recommentArray.count%4 != 0){
        for (int i=0; i<self.recommentArray.count%4; i++)
        {
            NSDictionary *personDic=[self.recommentArray objectAtIndex:i+indexPath.row*4];
            [useridListDic setValue:[NSNumber numberWithBool:YES] forKey:[personDic valueForKey:@"userid"]];
            [cell setImage:[personDic  valueForKey:@"uface"] distance:[personDic valueForKey:@"oldyears"] sex:[personDic  valueForKey:@"gender"] flag:NO forTile:i name:[personDic  valueForKey:@"pname"] onlineTime:[MyAppDataManager getTheSecondLastOnlineTime:[personDic  valueForKey:@"ltime"]] vip:[[personDic  valueForKey:@"userlevel"] integerValue]>0 verify:[[personDic  valueForKey:@"verify"] boolValue]];
        }
    }
    else{
        for (int i=0; i<4; i++)
        {
            NSDictionary *personDic=[self.recommentArray objectAtIndex:i+indexPath.row*4];
            [useridListDic setValue:[NSNumber numberWithBool:YES] forKey:[personDic valueForKey:@"userid"]];
            [cell setImage:[personDic  valueForKey:@"uface"] distance:[personDic valueForKey:@"oldyears"] sex:[personDic  valueForKey:@"gender"] flag:NO forTile:i name:[personDic valueForKey:@"pname"] onlineTime:[MyAppDataManager getTheSecondLastOnlineTime:[personDic  valueForKey:@"ltime"]] vip:[[personDic  valueForKey:@"userlevel"] integerValue]>0 verify:[[personDic  valueForKey:@"verify"] boolValue]];
        }
    }
    
    return  cell;
}
- (void)gridCell:(BanBu_GridCell *)cell didSelectedTile:(NSInteger)tileIndex realRow:(NSInteger)row
{
//    NSDictionary *buddy = [self.recommentArray objectAtIndex:row];
//    NSMutableDictionary  *parDic = [[NSMutableDictionary alloc] initWithDictionary:[buddy valueForKey:keyPersonalInfo]];
//    [parDic setValue:[MyAppDataManager getTheLastOnlineTime:[parDic valueForKey:@"ltime"]] forKey:@"ltime"];
//    BanBu_PersonDataViewController *peopleFfofile = [[BanBu_PersonDataViewController alloc] initWithProfile:parDic];
//    [self.leveyTabBarController hidesTabBar:YES animated:NO];
//    [parDic release];
//    [self.navigationController pushViewController:peopleFfofile animated:YES];
//    [peopleFfofile release];
}
- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    if(error)
    {
        [TKLoadingView showTkloadingAddedTo:self.view point:CGPointMake(160, 100) title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:1.5f];
    }
    else
    {
        if([AppComManager respondsDic:resDic isFunctionData:BanBu_Get_Video_Verify])
        {
            if([[resDic valueForKey:@"ok"] boolValue])
            {
                //                NSLog(@"%d",[[resDic valueForKey:@"list"] count]>=8?8:[[resDic valueForKey:@"list"] count]);
                [self.recommentArray removeAllObjects];
                [self.recommentArray addObjectsFromArray:[resDic valueForKey:@"list"]];
                [self.tableView reloadData];
            }
        }
        else if([AppComManager respondsDic:resDic isFunctionData:BanBu_Link_User_Tomany_Onekey])
        {
            if([[resDic valueForKey:@"ok"] boolValue])
            {
                NSMutableDictionary *uidDic = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
                [uidDic setValue:[resDic valueForKey:@"friendlist"] forKey:FriendShip];
                [uidDic setValue:[resDic valueForKey:@"friendsum"] forKey:@"friendsum"];
                [UserDefaults setValue:uidDic forKey:MyAppDataManager.useruid];
                [UserDefaults synchronize];
                
                [TKLoadingView showTkloadingAddedTo:self.view point:CGPointMake(160, 100)  title:NSLocalizedString(@"linkSuccess", nil) activityAnimated:NO duration:1.5f];
            }
            else
            {
                    [TKLoadingView showTkloadingAddedTo:self.view point:CGPointMake(160, 100)  title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:1.5f];
            }
        }
    }
}
@end
